package com.wuuxiang.polestar.admin.service.propertyitem;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.curator.utils.ZKPaths;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wuuxiang.polestar.admin.dao.IPropertyDao;
import com.wuuxiang.polestar.admin.dto.ConfigDto;
import com.wuuxiang.polestar.admin.dto.PropertyItem;
import com.wuuxiang.polestar.comm.constant.Constant;
import com.wuuxiang.utils.date.DateUtil;

/**
 * 项目名称：polestar <br>
 * 类描述： <br>
 * 创建人：夏鸿鹏 <br>
 * 创建时间：2017-11-28 20:04 <br>
 * 修改备注： <br>
 */
@Component
public class PropertyItemService implements IPropertyItemService {

	@Autowired
	private IPropertyDao propertyDao;

	@Override
	public Boolean checkExists(String teamName, String propertyName, String itemName) {
		return propertyDao.checkExists(
				ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_NODE, teamName, propertyName, itemName))
				|| propertyDao.checkExists(
						ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_TEMP_NODE, teamName, propertyName, itemName));
	}

	@Override
	public void createPropertyItem(String teamName, String propertyName, String itemName, String value, String remark,
			boolean isTemp) {
		if (isTemp) {
			propertyDao.createProperty(
					ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_TEMP_NODE, teamName, propertyName, itemName),
					value);
		} else {
			propertyDao.createProperty(
					ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_NODE, teamName, propertyName, itemName),
					value);
		}
		propertyDao.createProperty(ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_REMARK_NODE, teamName,
				propertyName, itemName, Constant.CONFIG_REMARK), remark);
	}

	@Override
	public void removePropertyItem(String teamName, String propertyName, String itemName) {
		propertyDao.deleteProperty(
				ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_NODE, teamName, propertyName, itemName));
		propertyDao.deleteProperty(
				ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_REMARK_NODE, teamName, propertyName, itemName));
		propertyDao.deleteProperty(
				ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_TEMP_NODE, teamName, propertyName, itemName));
	}
	
	@Override
	public void removePropertyItem(String nodeName,String teamName, String propertyName, String itemName) {
		propertyDao.deleteProperty(ZKPaths.makePath(Constant.ROOT_NODE, nodeName, teamName, propertyName, itemName));
	}
	
	@Override
	public void updatePropertyItem(String teamName, String propertyName, String itemName, String value, String remark) {
		// 未发布节点
		String tempNode = ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_TEMP_NODE, teamName, propertyName, itemName);
		if(propertyDao.checkExists(tempNode)) {
			propertyDao.updateProperty(tempNode, value);
		} else {
			propertyDao.createProperty(tempNode, value);
		}
		// 备注节点
		String remarkNode = ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_REMARK_NODE, teamName, propertyName,
				itemName, Constant.CONFIG_REMARK);
		if (propertyDao.checkExists(remarkNode)) {
			propertyDao.updateProperty(remarkNode, remark);
		} else {
			propertyDao.createProperty(remarkNode, remark);
		}
		
	}

	@Override
	public List<ConfigDto> getPropertyItemLst(String teamName, String propertyName) {
		List<PropertyItem> lst = propertyDao
				.findProperties(ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_NODE, teamName, propertyName));
		Map<String, String> tempMap = getPropertyMap(propertyDao.findProperties(
				ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_TEMP_NODE, teamName, propertyName)));
		List<ConfigDto> retLst = new ArrayList<ConfigDto>();
		for (PropertyItem propertyItem : lst) {
			String remarkNode = ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_REMARK_NODE, teamName,
					propertyName, propertyItem.getName(), Constant.CONFIG_REMARK);
			String userNameNode = ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_REMARK_NODE, teamName,
					propertyName, propertyItem.getName(), Constant.CONFIG_USER_NAME);
			String releaseTime = ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_REMARK_NODE, teamName,
					propertyName, propertyItem.getName(), Constant.CONFIG_RELEASE_TIME);
			ConfigDto configDto = new ConfigDto();
			configDto.setTeamName(teamName);
			configDto.setPropertyName(propertyName);
			configDto.setItemName(propertyItem.getName());
			configDto.setValue(propertyItem.getValue());
			configDto.setRemark(propertyDao.getProperty(remarkNode));
			configDto.setUsername(propertyDao.getProperty(userNameNode));
			configDto.setReleaseTime(propertyDao.getProperty(releaseTime));
			// 判断是否有未发布的数据
			if(StringUtils.isNotBlank(tempMap.get(propertyItem.getName()))) {
				configDto.setReleaseStatus(0);
				configDto.setTempValue(tempMap.get(propertyItem.getName()));
				tempMap.remove(propertyItem.getName());
			}
			retLst.add(configDto);
		}
		// 循环tempMap
		for (Map.Entry<String, String> entry : tempMap.entrySet()) {
			String remarkNode = ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_REMARK_NODE, teamName,
					propertyName, entry.getKey(), Constant.CONFIG_REMARK);
			ConfigDto configDto = new ConfigDto();
			configDto.setTeamName(teamName);
			configDto.setPropertyName(propertyName);
			configDto.setItemName(entry.getKey());
			configDto.setValue("");
			configDto.setRemark(propertyDao.getProperty(remarkNode));
			configDto.setUsername("");
			configDto.setReleaseTime("");
			configDto.setReleaseStatus(0);
			configDto.setTempValue(tempMap.get(entry.getKey()));
			retLst.add(configDto);
		}
		return retLst;
	}

	@Override
	public List<PropertyItem> getPropertyItemLstValue(String teamName, String propertyName) {
		return propertyDao.findProperties(ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_NODE, teamName, propertyName));
	}

	@Override
	public String getPropertyItemTempValue(String teamName, String propertyName, String itemName) {
		return propertyDao.getProperty(
				ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_TEMP_NODE, teamName, propertyName, itemName));
	}

	@Override
	public void releasePropertyItem(String teamName, String propertyName, String itemName, String value,String userName) {
		String nodeName = ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_NODE, teamName, propertyName, itemName);
		String tempNodeName = ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_TEMP_NODE, teamName, propertyName,
				itemName);
		String userNameNode = ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_REMARK_NODE, teamName,
				propertyName, itemName, Constant.CONFIG_USER_NAME);
		String releaseTime = ZKPaths.makePath(Constant.ROOT_NODE, Constant.CONFIG_REMARK_NODE, teamName,
				propertyName, itemName, Constant.CONFIG_RELEASE_TIME);
		// 更新发布的值
		if (propertyDao.checkExists(nodeName)) {
			propertyDao.updateProperty(nodeName, value);
		} else {
			propertyDao.createProperty(nodeName, value);
		}
		// 更新最后发布人
		if (propertyDao.checkExists(userNameNode)) {
			propertyDao.updateProperty(userNameNode, userName);
		} else {
			propertyDao.createProperty(userNameNode, userName);
		}
		// 更新最后修改时间
		String dateTime = DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
		if (propertyDao.checkExists(releaseTime)) {
			propertyDao.updateProperty(releaseTime, dateTime);
		} else {
			propertyDao.createProperty(releaseTime, dateTime);
		}
		// 删除未发布的值
		propertyDao.deleteProperty(tempNodeName);
	}

	
	/**********************************************************
	 * 方法说明： 获取属性的Map<br>
	 * 参数： @param list
	 * 参数： @return <br> 
	 * 创建人：liuwei  <br>
	 * 创建时间：2017年12月6日 上午8:56:39<br>   
	 * 修改备注： <br>
	 *********************************************************
	 */
	private Map<String, String> getPropertyMap(List<PropertyItem> list) {
		Map<String, String> result = new HashMap<String, String>();
		if(list!=null && list.size()>0) {
			for (PropertyItem propertyItem : list) {
				result.put(propertyItem.getName(), propertyItem.getValue());
			}
		}
		return result;
	}

} 
